﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SouthApps.MobileHealth.SDK.DAL.Model;
using System.Data.Objects;

namespace SouthApps.MobileHealth.SDK.DAL.DataAccess
{
    public class ServicioEspecialDao : DataAccessObject
    {
        #region Get

        public ServicioEspecial GetServicioEspecial(int servicioEspecialId)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    return entities.servicioespecial.FirstOrDefault(e =>
                        e.servicioEspecialId == servicioEspecialId);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        public List<ServicioEspecial> GetServiciosEspeciales()
        {
            return this.GetServiciosEspeciales(true);
        }

        public List<ServicioEspecial> GetServiciosEspeciales(bool activo)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    var query = from e in entities.servicioespecial
                                where e.activo == activo
                                select e;

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        public List<ServicioEspecial> GetServiciosEspecialesConLocacionesServiciosEspeciales(int financiadorId)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    var query = ((from e in entities.servicioespecial
                                join lse in entities.locacionservicioespecial on e.servicioEspecialId equals lse.servicioEspecialId
                                where lse.activo && e.activo && lse.financiadorId == financiadorId
                                select e) as ObjectQuery<ServicioEspecial>).Distinct();

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion

        #region Add

        public void AddServicioEspecial(ServicioEspecial servicioEspecial)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    entities.AddToservicioespecial(servicioEspecial);
                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion

        #region Modify

        public void ModifyServicioEspecial(ServicioEspecial servicioEspecial)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    ServicioEspecial oldServicioEspecial = entities.servicioespecial.FirstOrDefault(e => e.servicioEspecialId == servicioEspecial.servicioEspecialId);
                    oldServicioEspecial = servicioEspecial;

                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion
    }
}
